package com.example.day02.CustomView;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Calendar;

public class ClockView extends View {
    Paint paint,hpaint,linepaint;
    int hour,minute,seconds;//时分秒
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            getTime();
            invalidate();
            handler.sendEmptyMessageDelayed(1,1000);
        }
    };

    public ClockView(Context context) {
        super(context);
        initPaint();

    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        getTime();
        handler.sendEmptyMessageDelayed(1,1000);
    }
    public void getTime(){//获取时间
        Calendar calendar = Calendar.getInstance();
        hour=calendar.get(Calendar.HOUR);
        minute=calendar.get(Calendar.MINUTE);
        seconds=calendar.get(Calendar.SECOND);
    }
    public void initPaint(){
        paint=new Paint();
        paint.setStyle(Paint.Style.FILL);//设置实心
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);//抗锯齿

        hpaint=new Paint();
        hpaint.setStyle(Paint.Style.STROKE);//设置实心
        hpaint.setColor(Color.BLACK);
        hpaint.setAntiAlias(true);//抗锯齿

        linepaint=new Paint();
        linepaint.setStyle(Paint.Style.FILL);//设置实心
        linepaint.setColor(Color.RED);
        linepaint.setAntiAlias(true);//抗锯齿
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        getTime();
        //画表盘
        //绘制表的轴心
        canvas.drawCircle(getWidth()/2,getHeight()/2,10,hpaint);
        //绘制表盘
        hpaint.setStrokeWidth(5);//设置线条粗细
        hpaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2-10,hpaint);
        //内圈的表盘
        hpaint.setStrokeWidth(4);//设置线条粗细
        canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2-20,hpaint);
        //绘制刻度线
        linepaint.setColor(Color.RED);
        for (int i = 0; i < 12; i++) {
            canvas.save();
            canvas.rotate(360/12*(i+1),getWidth()/2,getHeight()/2);
            canvas.drawLine(getWidth()/2,30,getWidth()/2,40,linepaint);
            //canvas.drawText(String.valueOf(i+1),getWidth()/2-5,60,paint);//添加刻度文字
            //恢复画布
            canvas.restore();
        }
        //绘制时钟
        paint.setColor(Color.BLACK);
        canvas.save();
        canvas.rotate(hour*30+0.5f*minute,getWidth()/2,getHeight()/2);
        paint.setStrokeWidth(10);
        canvas.drawLine(getWidth()/2,getHeight()/2+getHeight()/20,getWidth()/2,getHeight()/2-getHeight()/5,paint);
        canvas.restore();
        //绘制分针
        paint.setColor(Color.GRAY);
        canvas.save();
        canvas.rotate(minute*6,getWidth()/2,getHeight()/2);
        paint.setStrokeWidth(6);
        canvas.drawLine(getWidth()/2,getHeight()/2+getHeight()/20,getWidth()/2,getHeight()/2-getHeight()/4,paint);
        canvas.restore();
        //绘制秒针
        paint.setColor(Color.RED);
        canvas.save();
        canvas.rotate(seconds*6,getWidth()/2,getHeight()/2);
        paint.setStrokeWidth(4);
        canvas.drawLine(getWidth()/2,getHeight()/2+20,getWidth()/2,getHeight()/2-getHeight()/3,paint);
        canvas.restore();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        获取使用自定义view时，在布局当中设定的，宽高的模式
        int wmode = MeasureSpec.getMode(widthMeasureSpec);
        int hmode = MeasureSpec.getMode(heightMeasureSpec);
//        获取到宽高的最大尺寸
        int wsize = MeasureSpec.getSize(widthMeasureSpec);
        int hsize = MeasureSpec.getSize(heightMeasureSpec);
//        判断xml文件中定义时，宽度采用的模式
        switch (wmode) {
            case MeasureSpec.AT_MOST:   //wrap_content
                if (hmode == MeasureSpec.AT_MOST) {
                    wsize = hsize = 400;
                }else {
                    wsize = hsize;
                }
                break;
            case MeasureSpec.EXACTLY: //march_parent   精确值
                wsize = hsize = Math.min(wsize,hsize);
                break;
        }
        //设置显示宽高
        setMeasuredDimension(wsize,hsize);
    }
}
